# Define options
set val(chan)            Channel/WirelessChannel     ;# channel type
set val(prop)            Propagation/TwoRayGround    ;# radio-propagation model
set val(netif)           Phy/WirelessPhy             ;# network interface type
set val(mac)             Mac/802_11                  ;# MAC type
set val(ifq)             Queue/DropTail/PriQueue     ;# interface queue type
set val(ll)              LL                          ;# link layer type
set val(ant)             Antenna/OmniAntenna         ;# antenna model
set val(ifqlen)          50                          ;# max packet in ifq
set val(nn)              55                          ;# number of mobilenodes
set val(rp)              AODV                        ;# routing protocol
set val(start)           2                           ;# Tempo máximo de inicio
set val(x)               50                          ;# X dimension of topography
set val(y)               50                          ;# Y dimension of topography 
set val(stop)            2400                        ;# time of simulation end 2400 - 40m
set val(p_acesso)        0			     ;# Qual o sensor que representa o ponto de acesso	
Agent/MScoop set num_sensores_    $val(nn)           ;# Numero sensores(incluindo PA)
Agent/MScoop set time_dados_      15		     ;# time between data generation 10-15
Agent/MScoop set time_sumarios_   110		     ;# time between sumarios
Agent/MScoop set time_sa_         240		     ;# time between sa
Agent/MScoop set time_consultas_  15		     ;# time between consultas
Agent/MScoop set size_buffer_     30                 ;# Tamanho do buffer de dados
Agent/MScoop set histo_nBins_     10                  ;# Número de bins para o histograma
Agent/MScoop set sa_nBins_        15                 ;# Número de bins para a SA
Agent/MScoop set max_piggy_       15                  ;# Tempo máximo de piggybacking
Agent/MScoop set const_piggy_     0.7                ;# Constante de decaida para o calculo do piggybacking
Agent/MScoop set tipo_dados_      0                  ;# Tipo de distribuição de dados
Agent/MScoop set tipo_dist_sa_    0	                  ;# Tipo de distribuição de intervalos da SA(0-simples;1-equidepth)
Agent/MScoop set tipo_scoop_      1                  ;# Tipo de Cálculo de SA(0-simples;1-Consultas pares)
Agent/MScoop set ponto_acesso_    $val(p_acesso)     ;# Qual o sensor que representa o ponto de acesso
Agent/MScoop set perc_con_duplas_ 90		     ;# Frequencia de producao de consultas pelo sensor perc_con_duplas_
Agent/MScoop set radio_w_range_   10.0		     ;# Alcance do rádio
Agent/MScoop set threshold_data_  0.1		     ;# threshold para envio ou nao dos dados
Agent/MScoop set threshold_histo_ 0.1		     ;# threshold para envio ou não dos histogramas


#Creating simulation:
set ns              [new Simulator]

#Creating nam and trace file:
set tracefd       [open random_wireless.tr w]
set namtrace      [open random_wireless.nam w]   

$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# set up topography object
set topo       [new Topography]

$topo load_flatgrid $val(x) $val(y)

set god_ [create-god $val(nn)]

# configure the nodes
        $ns node-config -adhocRouting $val(rp) \
                   -llType $val(ll) \
                   -macType $val(mac) \
                   -ifqType $val(ifq) \
                   -ifqLen $val(ifqlen) \
                   -antType $val(ant) \
                   -propType $val(prop) \
                   -phyType $val(netif) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                   
## Creating node objects..         
for {set i 0} {$i < $val(nn) } { incr i } {
            set node_($i) [$ns node]     
      }
## Intel Berkeley Research lab Location
$node_(0) set X_ 0
$node_(0) set Y_ 0

$node_(1) set X_ 21.5
$node_(1) set Y_ 23

$node_(2) set X_ 24.5
$node_(2) set Y_ 20

$node_(3) set X_ 19.5
$node_(3) set Y_ 19

$node_(4) set X_ 22.5
$node_(4) set Y_ 15

$node_(5) set X_ 24.5
$node_(5) set Y_ 12

$node_(6) set X_ 19.5
$node_(6) set Y_ 13

$node_(7) set X_ 22.5
$node_(7) set Y_ 8

$node_(8) set X_ 24.5
$node_(8) set Y_ 4

$node_(9) set X_ 21.5
$node_(9) set Y_ 2

$node_(10) set X_ 19.5
$node_(10) set Y_ 5

$node_(11) set X_ 16.5
$node_(11) set Y_ 3

$node_(12) set X_ 13.5
$node_(12) set Y_ 1

$node_(13) set X_ 12.5
$node_(13) set Y_ 5

$node_(14) set X_ 8.5
$node_(14) set Y_ 6

$node_(15) set X_ 5.5
$node_(15) set Y_ 3

$node_(16) set X_ 1.5
$node_(16) set Y_ 2

$node_(17) set X_ 1.5
$node_(17) set Y_ 8

$node_(18) set X_ 5.5
$node_(18) set Y_ 10

$node_(19) set X_ 3.5
$node_(19) set Y_ 13

$node_(20) set X_ 0.5
$node_(20) set Y_ 17

$node_(21) set X_ 4.5
$node_(21) set Y_ 18

$node_(22) set X_ 1.5
$node_(22) set Y_ 23

$node_(23) set X_ 6
$node_(23) set Y_ 24

$node_(24) set X_ 1.5
$node_(24) set Y_ 30

$node_(25) set X_ 4.5
$node_(25) set Y_ 30

$node_(26) set X_ 7.5
$node_(26) set Y_ 31

$node_(27) set X_ 8.5
$node_(27) set Y_ 26

$node_(28) set X_ 10.5
$node_(28) set Y_ 31

$node_(29) set X_ 12.5
$node_(29) set Y_ 26

$node_(30) set X_ 13.5
$node_(30) set Y_ 31

$node_(31) set X_ 15.5
$node_(31) set Y_ 28

$node_(32) set X_ 17.5
$node_(32) set Y_ 31

$node_(33) set X_ 19.5
$node_(33) set Y_ 26

$node_(34) set X_ 21.5
$node_(34) set Y_ 30

$node_(35) set X_ 24.5
$node_(35) set Y_ 27

$node_(36) set X_ 26.5
$node_(36) set Y_ 31

$node_(37) set X_ 27.5
$node_(37) set Y_ 26

$node_(38) set X_ 30.5
$node_(38) set Y_ 31

$node_(39) set X_ 30.5
$node_(39) set Y_ 26

$node_(40) set X_ 33.5
$node_(40) set Y_ 28

$node_(41) set X_ 36.5
$node_(41) set Y_ 30

$node_(42) set X_ 39.5
$node_(42) set Y_ 30

$node_(43) set X_ 35.5
$node_(43) set Y_ 24

$node_(44) set X_ 40.5
$node_(44) set Y_ 22

$node_(45) set X_ 37.5
$node_(45) set Y_ 19

$node_(46) set X_ 34.5
$node_(46) set Y_ 16

$node_(47) set X_ 39.5
$node_(47) set Y_ 14

$node_(48) set X_ 35.5
$node_(48) set Y_ 10

$node_(49) set X_ 39.5
$node_(49) set Y_ 6

$node_(50) set X_ 38.5
$node_(50) set Y_ 1

$node_(51) set X_ 35.5
$node_(51) set Y_ 4

$node_(52) set X_ 31.5
$node_(52) set Y_ 6

$node_(53) set X_ 28.5
$node_(53) set Y_ 5

$node_(54) set X_ 26.5
$node_(54) set Y_ 2


#[$node_($i) node-addr]
for {set i 0} {$i < $val(nn)} {incr i} {
	set smscoop_($i) [new Agent/MScoop [$node_($i) node-addr]  ] 
	 
	$ns attach-agent $node_($i) $smscoop_($i)
}
set pa $val(p_acesso)
for {set i 0} {$i < $val(nn)} {incr i} {
	if {$i != $pa} then {$ns connect $smscoop_($i) $smscoop_($pa)}
}



# Define node initial position in nam
for {set i 0} {$i < $val(nn)} { incr i } {
# 30 defines the node size for nam
$ns initial_node_pos $node_($i) 30
}


# start procedure..
for {set i 0} {$i < $val(nn) } { incr i } {
    #set start_time [expr rand()*$val(start)]
    set start_time $val(start)
    if {$i != $pa} then {$ns at $start_time "$smscoop_($i) start"}
    if {$i == $pa} then {$ns at 0 "$smscoop_($i) start"}
}

# dynamic destination setting procedure..
#$ns at 0.0 "destination"
#proc destination {} {
#      global ns val node_
#      set time 1.0
#      set now [$ns now]
#      for {set i 0} {$i<$val(nn)} {incr i} {
#            set xx [expr rand()*$val(x)]
#            set yy [expr rand()*$val(y)]
#            $ns at $now "$node_($i) setdest $xx $yy 10.0"
#      }
#      $ns at [expr $now+$time] "destination"
#}

# Define a 'finish' procedure
proc finish {} {
        global ns tracefd
        $ns flush-trace
        close $tracefd
        #exec nam random_wireless.nam &
        exit 0
}

#stop procedure..
# start procedure..
for {set i 0} {$i < $val(nn) } { incr i } {
    $ns at $val(stop) "$smscoop_($i) stop";
}

$ns at $val(stop) "finish"

proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
#exec nam random_wireless.nam &
}

$ns run
